package com.junchenrobot.mes.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * Springboot 容器
 *
 * @author SeaWave 2022/03/23
 */
@Component
public class SpringContentUtil implements ApplicationContextAware {
    
    private static ApplicationContext applicationContext;
    
    /**
     * 获取对象
     *
     * @param name 对象名字
     * @return Object
     */
    public static Object getBean(String name) {
        return applicationContext.getBean(name);
    }
    
    /**
     * 获取对象
     *
     * @param name  名字
     * @param clazz 类型
     * @return T
     */
    public static <T> T getBean(String name, Class<T> clazz) {
        return applicationContext.getBean(name, clazz);
    }
    
    /**
     * 设置应用程序上下文
     *
     * @param applicationContext 应用程序上下文
     * @throws BeansException 豆子例外
     * @see ApplicationContextAware#setApplicationContext(ApplicationContext)
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContentUtil.applicationContext = applicationContext;
    }
    
}
